{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "39d21a91-2c31-48de-9172-43a66ee5354a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "568c6b83-d575-4baa-a384-43e5ce081844",
   "metadata": {},
   "outputs": [],
   "source": [
    "data= pd.read_csv(\"boston.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f050c372-b14e-4d64-9e53-4a3a7229c1b0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CRIM</th>\n",
       "      <th>ZN</th>\n",
       "      <th>INDUS</th>\n",
       "      <th>CHAS</th>\n",
       "      <th>NOX</th>\n",
       "      <th>RM</th>\n",
       "      <th>AGE</th>\n",
       "      <th>DIS</th>\n",
       "      <th>RAD</th>\n",
       "      <th>TAX</th>\n",
       "      <th>PIRATIO</th>\n",
       "      <th>B</th>\n",
       "      <th>LSTAT</th>\n",
       "      <th>MEDV</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.00632</td>\n",
       "      <td>18.0</td>\n",
       "      <td>2.31</td>\n",
       "      <td>0</td>\n",
       "      <td>0.538</td>\n",
       "      <td>6.575</td>\n",
       "      <td>65.2</td>\n",
       "      <td>4.0900</td>\n",
       "      <td>1</td>\n",
       "      <td>296</td>\n",
       "      <td>15.3</td>\n",
       "      <td>396.90</td>\n",
       "      <td>4.98</td>\n",
       "      <td>24.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.02731</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>6.421</td>\n",
       "      <td>78.9</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2</td>\n",
       "      <td>242</td>\n",
       "      <td>17.8</td>\n",
       "      <td>396.90</td>\n",
       "      <td>9.14</td>\n",
       "      <td>21.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.02729</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.07</td>\n",
       "      <td>0</td>\n",
       "      <td>0.469</td>\n",
       "      <td>7.185</td>\n",
       "      <td>61.1</td>\n",
       "      <td>4.9671</td>\n",
       "      <td>2</td>\n",
       "      <td>242</td>\n",
       "      <td>17.8</td>\n",
       "      <td>392.83</td>\n",
       "      <td>4.03</td>\n",
       "      <td>34.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.03237</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>6.998</td>\n",
       "      <td>45.8</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3</td>\n",
       "      <td>222</td>\n",
       "      <td>18.7</td>\n",
       "      <td>394.63</td>\n",
       "      <td>2.94</td>\n",
       "      <td>33.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.06905</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2.18</td>\n",
       "      <td>0</td>\n",
       "      <td>0.458</td>\n",
       "      <td>7.147</td>\n",
       "      <td>54.2</td>\n",
       "      <td>6.0622</td>\n",
       "      <td>3</td>\n",
       "      <td>222</td>\n",
       "      <td>18.7</td>\n",
       "      <td>396.90</td>\n",
       "      <td>5.33</td>\n",
       "      <td>36.2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      CRIM    ZN  INDUS  CHAS    NOX     RM   AGE     DIS  RAD  TAX  PIRATIO  \\\n",
       "0  0.00632  18.0   2.31     0  0.538  6.575  65.2  4.0900    1  296     15.3   \n",
       "1  0.02731   0.0   7.07     0  0.469  6.421  78.9  4.9671    2  242     17.8   \n",
       "2  0.02729   0.0   7.07     0  0.469  7.185  61.1  4.9671    2  242     17.8   \n",
       "3  0.03237   0.0   2.18     0  0.458  6.998  45.8  6.0622    3  222     18.7   \n",
       "4  0.06905   0.0   2.18     0  0.458  7.147  54.2  6.0622    3  222     18.7   \n",
       "\n",
       "        B  LSTAT  MEDV  \n",
       "0  396.90   4.98  24.0  \n",
       "1  396.90   9.14  21.6  \n",
       "2  392.83   4.03  34.7  \n",
       "3  394.63   2.94  33.4  \n",
       "4  396.90   5.33  36.2  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaa0dce3-1968-4605-a62a-8a99fe4237cc",
   "metadata": {},
   "source": [
    "# No\t属性\t数据类型\t字段描述x\n",
    "# 1\tCRIM\tFloat\t城镇人均犯罪率\n",
    "# 2\tZN\tFloat\t占地面积超过2.5万平方英尺的住宅用地比例\n",
    "# 3\tINDUS\tFloat\t城镇非零售业务地区的比例\n",
    "# 4\tCHAS\tInteger\t查尔斯河虚拟变量 (= 1 如果土地在河边；否则是0)\n",
    "# 5\tNOX\tFloat\t一氧化氮浓度（每1000万份）\n",
    "# 6\tRM\tFloat\t平均每居民房数\n",
    "# 7\tAGE\tFloat\t在1940年之前建成的所有者占用单位的比例\n",
    "# 8\tDIS\tFloat\t与五个波士顿就业中心的加权距离\n",
    "# 9\tRAD\tInteger\t辐射状公路的可达性指数\n",
    "# 10\tTAX\tFloat\t每10,000美元的全额物业税率\n",
    "# 11\tPTRATIO\tFloat\t城镇师生比例\n",
    "# 12\tB\tFloat\t1000（Bk - 0.63）^ 2其中Bk是城镇黑人的比例\n",
    "# 13\tLSTAT\tFloat\t人口中地位较低人群的百分数\n",
    "# 14\tMEDV\tFloat\t（目标变量/类别属性）以1000美元计算的自有住房的中位数\r",
    "859"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "90a15cf2-fbf5-4003-b44b-723503082033",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 506 entries, 0 to 505\n",
      "Data columns (total 14 columns):\n",
      " #   Column   Non-Null Count  Dtype  \n",
      "---  ------   --------------  -----  \n",
      " 0   CRIM     506 non-null    float64\n",
      " 1   ZN       506 non-null    float64\n",
      " 2   INDUS    506 non-null    float64\n",
      " 3   CHAS     506 non-null    int64  \n",
      " 4   NOX      506 non-null    float64\n",
      " 5   RM       506 non-null    float64\n",
      " 6   AGE      506 non-null    float64\n",
      " 7   DIS      506 non-null    float64\n",
      " 8   RAD      506 non-null    int64  \n",
      " 9   TAX      506 non-null    int64  \n",
      " 10  PIRATIO  506 non-null    float64\n",
      " 11  B        506 non-null    float64\n",
      " 12  LSTAT    506 non-null    float64\n",
      " 13  MEDV     506 non-null    float64\n",
      "dtypes: float64(11), int64(3)\n",
      "memory usage: 55.5 KB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "900fdfa3-a6cb-464b-a297-1d0237123aff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LSTAT     -0.737663\n",
       "PIRATIO   -0.507787\n",
       "INDUS     -0.483725\n",
       "TAX       -0.468536\n",
       "NOX       -0.427321\n",
       "CRIM      -0.388305\n",
       "RAD       -0.381626\n",
       "AGE       -0.376955\n",
       "CHAS       0.175260\n",
       "DIS        0.249929\n",
       "B          0.333461\n",
       "ZN         0.360445\n",
       "RM         0.695360\n",
       "MEDV       1.000000\n",
       "Name: MEDV, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.corr()['MEDV'].sort_values()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1bb4b8ca-896d-48c7-a3f1-face7936ee89",
   "metadata": {},
   "outputs": [],
   "source": [
    "x ,r = data[data.columns.delete(-1)], data['MEDV']\n",
    "x_train, x_test, r_train, r_test = train_test_split(x, r, test_size=0.2, random_state=888)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "22350359-1088-4de2-a07c-bfe6cab5d23c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(404, 13) (404,)\n",
      "(102, 13) (102,)\n"
     ]
    }
   ],
   "source": [
    "print(x_train.shape,r_train.shape)\n",
    "print(x_test.shape,r_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e53d2d57-8599-491d-ad1e-1950c6bbb998",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SCORE:0.7559\n",
      "RMSE:4.3708\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([-1.19007229e-01,  3.64055815e-02,  1.68552680e-02,  2.29397031e+00,\n",
       "       -1.60706448e+01,  3.72371469e+00,  9.22765437e-03, -1.30674803e+00,\n",
       "        3.43072685e-01, -1.45830386e-02, -9.73486692e-01,  7.89797436e-03,\n",
       "       -5.72555056e-01])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linear_model = LinearRegression()\n",
    "linear_model.fit(x_train, r_train)\n",
    "coef = linear_model.coef_#回归系数\n",
    "line_pre = linear_model.predict(x_test)\n",
    "print('SCORE:{:.4f}'.format(linear_model.score(x_test, r_test)))\n",
    "print('RMSE:{:.4f}'.format(np.sqrt(mean_squared_error(r_test, line_pre))))\n",
    "coef\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd94dda9-b40a-4b75-b070-7f7521f83a7a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
